package com.zcf.service;
import java.text.SimpleDateFormat;
import java.util.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zcf.common.json.Body;
import com.zcf.mapper.UserRecordMapper;
import com.zcf.pojo.UserRecord;
import com.zcf.vo.out.LayuiJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @author jj
 * @date 2019/11/22 9:20
 */
@Service
public class UserRecordServiceImpl {
    @Autowired
    UserRecordMapper userRecordMapper;
    @Autowired
    RechargeServiceImpl rechargeService;
    /**
    * @description :查询战绩
    * @param userid 
    * @param type 
    * @param tiao
    * @date : 2019/11/29 15:38
    * @author : jj
    */
    public Body userRecordList(Long userid,Integer type, int tiao){
        List<UserRecord> userRecords = userRecordMapper.userRecordList(userid,type,tiao);
        List list=new ArrayList();
        for (UserRecord user:userRecords){
            String str = "yyy-MM-dd HH:mm:ss";
            SimpleDateFormat sdf = new SimpleDateFormat(str);
            Map <String, Object> map=new HashMap<> ();
            List<UserRecord> userRecordList = userRecordMapper.selectList(new EntityWrapper<UserRecord>().eq("uuid",user.getUuid()));
            UserRecord userRecord = userRecordList.get(0);
            map.put("roomid",userRecord.getRoomid());
            map.put("users",userRecordList);
            map.put("time", sdf.format(userRecord.getCreateTime()));
            map.put("gameType", userRecord.getGameType());
            map.put("roomGrade", userRecord.getRoomGrade());
            list.add(map);
        }
        return  userRecords.size()<1 ? Body.newInstance(300,"暂无数据"):Body.newInstance(list);
    }


    /**
     * @description :查询战绩
     * @param userid
     * @date : 2019/11/29 15:38
     * @author : jj
     */
    public int userSum(Long userid){
        Calendar calendar1 = Calendar.getInstance();
        calendar1.set(calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH), calendar1.get(Calendar.DAY_OF_MONTH),
                0, 0, 0);
        Date beginOfDate = calendar1.getTime();
        Calendar calendar2 = Calendar.getInstance();
        calendar1.set(calendar2.get(Calendar.YEAR), calendar2.get(Calendar.MONTH), calendar2.get(Calendar.DAY_OF_MONTH),
                23, 59, 59);
        Date endOfDate = calendar1.getTime();
        List<UserRecord> userRecords = userRecordMapper.userSum(userid,beginOfDate, endOfDate);
        if (userRecords.size()>0) {
            int sum = userRecords.stream().mapToInt(UserRecord::getSumWinnumber).sum();
                return sum;
        }
        return 0;
    }

    public LayuiJson waste(){
        LayuiJson lj=new LayuiJson();
        Calendar calendar1 = Calendar.getInstance();
        calendar1.set(calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH), calendar1.get(Calendar.DAY_OF_MONTH),
                0, 0, 0);
        Date beginOfDate = calendar1.getTime();
        Calendar calendar2 = Calendar.getInstance();
        calendar1.set(calendar2.get(Calendar.YEAR), calendar2.get(Calendar.MONTH), calendar2.get(Calendar.DAY_OF_MONTH),
                23, 59, 59);
        Date endOfDate = calendar1.getTime();
        List<UserRecord> userRecords = userRecordMapper.selectByExample(beginOfDate, endOfDate);
        int sumnum = rechargeService.toDayRechargely();
        Map<String, Integer> map = new HashMap<String, Integer>();
        List<Map<String, Integer>> list = new ArrayList<>();
        if (userRecords.size()>0){
            int sum = userRecords.stream().mapToInt(UserRecord::getSumWinnumber).sum();
            map.put("sum", sum);
            map.put("sumnum",sumnum);
            list.add(map);
            if(map != null){
                lj.setCode(0);
                lj.setCount(null);
                lj.setData(list);
                lj.setMsg("成功");
                return lj;
            }
        }else {
            map.put("sum", 0);
            map.put("sumnum",sumnum);
            list.add(map);
            lj.setCode(0);
            lj.setCount(null);
            lj.setData(list);
            lj.setMsg("成功");
        }
        return lj;
    }
}
